class Timeline {
    public timerId: any = null;
    public millisecond = 0;

    constructor(
        public start: number,
        public end: number,
        public func: (millisecond: number) => void
    ) {}

    run(interval = 30) {
        this.timerId = setInterval(() => {
            if (this.millisecond >= this.end) {
                this.stop();
                return;
            }
            this.millisecond = this.millisecond + interval;
            this.func(this.millisecond);
        }, interval);
        return this;
    }

    stop() {
        clearInterval(this.timerId);
    }

    current(millisecond: number) {
        this.millisecond = millisecond;
        this.stop();
        this.func(this.millisecond);
    }

    in(start: number, end: number) {
        return this.millisecond >= start && this.millisecond <= end;
    }
}

export default function timeline(
    start: number,
    end: number,
    func: (millisecond: number) => void
) {
    return new Timeline(start, end, func);
}
